package com.liu.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

@SuppressWarnings({"all"})
public class UDPReceiverA {
    public static void main(String[] args) throws IOException {
        //创建一个DatagramSocket对象，准备在9999接收数据
        DatagramSocket datagramSocket = new DatagramSocket(9999);
        //构建DatagramPacket对象,UDP协议一个数据包最大是64k
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        //调用接收方法
        datagramSocket.receive(datagramPacket);
        //拆包，取出数据
        int length = datagramPacket.getLength();
        System.out.println("接收到的数据长度：" + length);
        byte[] data = datagramPacket.getData();
        String s = new String(data, 0, length);
        System.out.println(s);
        datagramSocket.close();
    }
}
